package com.zhuangfei.hos_timetableview.ability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.adapter.NonViewAdapter;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.model.Schedule;
import com.zhuangfei.timetable.model.ScheduleSupport;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用工具操作
 * 实现课程的罗列以及颜色的显示
 * 显示的颜色使用的默认的颜色池中的颜色
 */
public class NonViewAbility extends Ability {

    Button moreButton;
    List<Schedule> schedules;
    ListContainer listContainer;
    NonViewAdapter adapter;
    String[] popupMenus;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_non_view);
        moreButton = (Button) findComponentById(ResourceTable.Id_id_more);
        moreButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showPopmenu();
            }
        });
        schedules = new ArrayList<>();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_id_listcontainer);
        adapter = new NonViewAdapter(this, schedules);
        listContainer.setItemProvider(adapter);
        all();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        if (popupMenus == null) {
            popupMenus = new String[]{"全部课程", "第1周有课的课程", "第1周星期一有课的课程"};
        }
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .atView(moreButton)
                .asAttachList(popupMenus, null,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (text) {
                                    case "全部课程":
                                        all();
                                        break;
                                    case "第1周有课的课程":
                                        haveTime();
                                        break;
                                    case "第1周星期一有课的课程":
                                        haveTimeWithMonday();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }, 0, 0).show();
    }

    public List<Schedule> getData() {
        List<Schedule> list = ScheduleSupport.transform(SubjectRepertory.loadDefaultSubjects());
        list = ScheduleSupport.getColorReflect(list); // 分配颜色
        return list;
    }

    /**
     * 获取所有课程
     */
    protected void all() {
        schedules.clear();
        schedules.addAll(getData());
        adapter.notifyDataChanged();
    }

    /**
     * 获取第一周有课的课程并显示出来
     */
    protected void haveTime() {
        List<Schedule> result = new ArrayList<>();
        List<Schedule>[] arr = ScheduleSupport.splitSubjectWithDay(getData());
        for (int i = 0; i < arr.length; i++) {
            List<Schedule> tmpList = arr[i];
            for (Schedule schedule : tmpList) {
                if (ScheduleSupport.isThisWeek(schedule, 1)) {
                    result.add(schedule);
                }
            }
        }
        schedules.clear();
        schedules.addAll(result);
        adapter.notifyDataChanged();
        listContainer.invalidate();
    }

    /**
     * 显示第一周周一有课的课程
     */
    protected void haveTimeWithMonday() {
        List<Schedule> tmpList = ScheduleSupport.getHaveSubjectsWithDay(
                getData(), 1, 0);
        schedules.clear();
        schedules.addAll(tmpList);
        adapter.notifyDataChanged();
        listContainer.invalidate();
    }

}
